// pages/notification-settings/notification-settings.ts
import { MerchantAPI } from '../../utils/api'

interface NotificationSettingsData {
  statusBarHeight: number;
  loading: boolean;
  
  // 消息类型开关
  enableCustomerMessage: boolean;
  enablePlatformMessage: boolean;
  enableGroupMessage: boolean;
  
  // 提醒方式
  enableSound: boolean;
  enableVibration: boolean;
  
  // 免打扰时段
  quietHoursEnabled: boolean;
  quietHoursStart: string;
  quietHoursEnd: string;
}

Page({
  data: {
    statusBarHeight: 0,
    loading: false,
    
    // 消息类型开关
    enableCustomerMessage: true,
    enablePlatformMessage: true,
    enableGroupMessage: true,
    
    // 提醒方式
    enableSound: true,
    enableVibration: true,
    
    // 免打扰时段
    quietHoursEnabled: false,
    quietHoursStart: '22:00',
    quietHoursEnd: '08:00'
  } as NotificationSettingsData,

  onLoad() {
    this.setData({
      statusBarHeight: wx.getSystemInfoSync().statusBarHeight || 0
    });
    
    // 加载通知设置
    this.loadNotificationSettings();
  },

  /**
   * 加载通知设置
   */
  async loadNotificationSettings() {
    wx.showLoading({ title: '加载中...' });
    
    try {
      const response = await MerchantAPI.getMessageNotificationSettings();
      
      if (response.code === 200 && response.data) {
        const settings = response.data;
        
        this.setData({
          enableCustomerMessage: settings.enableCustomerMessage !== false,
          enablePlatformMessage: settings.enablePlatformMessage !== false,
          enableGroupMessage: settings.enableGroupMessage !== false,
          enableSound: settings.enableSound !== false,
          enableVibration: settings.enableVibration !== false,
          quietHoursStart: settings.quietHoursStart || '22:00',
          quietHoursEnd: settings.quietHoursEnd || '08:00',
          quietHoursEnabled: !!(settings.quietHoursStart && settings.quietHoursEnd)
        });
      }
    } catch (error) {
      console.error('加载通知设置失败:', error);
      wx.showToast({
        title: '加载设置失败',
        icon: 'none'
      });
    } finally {
      wx.hideLoading();
    }
  },

  /**
   * 保存通知设置
   */
  async saveNotificationSettings() {
    this.setData({ loading: true });
    
    try {
      const params = {
        enableCustomerMessage: this.data.enableCustomerMessage,
        enablePlatformMessage: this.data.enablePlatformMessage,
        enableGroupMessage: this.data.enableGroupMessage,
        enableSound: this.data.enableSound,
        enableVibration: this.data.enableVibration,
        quietHoursStart: this.data.quietHoursEnabled ? this.data.quietHoursStart : undefined,
        quietHoursEnd: this.data.quietHoursEnabled ? this.data.quietHoursEnd : undefined
      };
      
      const response = await MerchantAPI.updateMessageNotificationSettings(params);
      
      if (response.code === 200) {
        wx.showToast({
          title: '保存成功',
          icon: 'success'
        });
        
        // 延迟返回
        setTimeout(() => {
          wx.navigateBack();
        }, 1500);
      }
    } catch (error) {
      console.error('保存通知设置失败:', error);
      wx.showToast({
        title: '保存失败',
        icon: 'none'
      });
    } finally {
      this.setData({ loading: false });
    }
  },

  // 返回上一页
  goBack() {
    wx.navigateBack();
  },

  // 消息类型开关切换
  onCustomerMessageChange(e: any) {
    this.setData({ enableCustomerMessage: e.detail.value });
  },

  onPlatformMessageChange(e: any) {
    this.setData({ enablePlatformMessage: e.detail.value });
  },

  onGroupMessageChange(e: any) {
    this.setData({ enableGroupMessage: e.detail.value });
  },

  // 提醒方式切换
  onSoundChange(e: any) {
    this.setData({ enableSound: e.detail.value });
  },

  onVibrationChange(e: any) {
    this.setData({ enableVibration: e.detail.value });
  },

  // 免打扰时段切换
  onQuietHoursChange(e: any) {
    this.setData({ quietHoursEnabled: e.detail.value });
  },

  // 免打扰开始时间
  onQuietStartTimeChange(e: any) {
    this.setData({ quietHoursStart: e.detail.value });
  },

  // 免打扰结束时间
  onQuietEndTimeChange(e: any) {
    this.setData({ quietHoursEnd: e.detail.value });
  },

  // 保存设置按钮
  onSave() {
    this.saveNotificationSettings();
  }
});
